//
//  AppDelegate.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/4.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    //根据登录状态切换根控制器
    private func getRootViewController ()->UIViewController{
        
        return NLUserAccountViewModel.shared.isLogin ? NLWelcomeViewController():NLTabBarController()
        
    }
    
    //注册通知
    private func registerNoti (){
        //结构体对象
        NotificationCenter.default.addObserver(self, selector: #selector(changeRootVC), name: NSNotification.Name.AppChangeRootVC, object: nil)
        
    }
    
    //MARK:flag 纯CV
    @objc private func changeRootVC (noti:Notification){
        
        //设置window的根视图控制器
        //1. 获取通知对象的object参数 --> 控制器名称
        let vcName = noti.object as! String
        print(vcName)
        //1.1 获取bundleName 拼接类型名称
        let productName = Bundle.main.infoDictionary!["CFBundleName"] as! String
        let className = productName + "." + vcName
        
        //2. 将字符串类型的控制器名称转换为类型
        ///NSClassFromString(className) as! UIViewController 转换的是一个对象
        //fatal error: unexpectedly found nil while unwrapping an Optional value
        print(self)
        let classType = NSClassFromString(className) as! UIViewController.Type
        //3. 实例化控制器对象
        let rootVC = classType.init()
        //4. 设置根视图控制器
        window?.rootViewController = rootVC
    }
    
    //移除通知(无意义)
    deinit{
        
        NotificationCenter.default.removeObserver(self)
        
    }
    
    
    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.rootViewController = getRootViewController()
        
        window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        window?.makeKeyAndVisible()
        
        registerNoti()
        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

